<template>
    <div>
        <el-dialog :title="dialogtitle" width="1000px" class="icon-dialog" :visible.sync="show" :before-close="closeForm"
            append-to-body @open="open">
            <el-form ref="form" :size="size" :model="form" :rules="rules"
                :label-width="$store.getters.device !== 'mobile' ? '16%' : '90px'">
                <el-row>
                    <el-col :span="24">
                        <el-form-item label="名称" prop="title">
                            <el-input v-model="form.title" auto-complete="off" clearable placeholder="请输入名称" />
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="24">
                        <el-form-item label="类别">
                            <el-select v-model="form.cid" style="width:100%" filterable clearable placeholder="请选择">
                                <el-option v-for="(item, i) in cidarray" :key="i" :label="item.key" :value="item.val" />
                            </el-select>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="24">
                        <el-form-item label="内容" prop="content">
                            <WangEditor v-if="show" :wang-editor-content.sync="form.content" />
                        </el-form-item>
                        
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="24">
                        <el-form-item label="状态">
                            <el-switch v-model="form.status" :active-value="1" :inactive-value="0" />
                        </el-form-item>
                    </el-col>
                </el-row>

                <el-row>
                    <el-col :span="24">
                        <el-form-item label="排序">
                            <el-input-number v-model="form.sort" controls-position="right" style="width:200px;"
                                auto-complete="off" clearable :min="0" placeholder="排序" />
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button :loading="loading" type="primary" @click="submit">
                    <span v-if="!loading">确 定</span>
                    <span v-else>提 交 中...</span>
                </el-button>
                <el-button @click="closeForm">取 消</el-button>
            </div>
        </el-dialog>
    </div>
</template>
<script>
import WangEditor from '@/components/common/WangEditor.vue'
export default {
    name: 'Articleupdate', 
    components: { WangEditor },
    props: {
        show: {
            type: Boolean,
            default: false
        },
        opentype: {
            type: String,
            default: 'add'
        },
        size: {
            type: String,
            default: 'small'
        },
        info: {
            type: Object
        }
    },
    data() {
        return {
            dialogtitle: '',
            cidarray: [],
            form: {
                title: '',
                status: 1,
                content: '',
            },
            loading: false,
            rules: {
                title: [{
                    required: true,
                    message: '显示文字不能为空',
                    trigger: 'blur'
                }],
                content: [{
                    required: true,
                    message: '内容不能为空',
                    trigger: 'blur'
                }]
            }
        }
    },
    watch: {
        show(val) {
            if (val) {
                this.$api.post('/article/getField').then(res => {
                    this.cidarray = res.data.cidarray
                })
            }
        }
    },
    methods: {
        open() {
            if (this.opentype == 'update') {
                this.dialogtitle = '修改'
                this.form = this.info
            } else {
                this.dialogtitle = '添加'
                this.form = {
                    sort: 100,
                    status: 1
                }
                // console.log(this.form);
            }
        },
        submit() {
            this.$refs['form'].validate(valid => {
                if (valid) {
                    this.loading = true

                    this.$api.post('/article/update', this.form).then(res => {
                        this.$message({
                            message: res.msg,
                            type: 'success'
                        })
                        this.$emit('refesh_list')
                        this.closeForm()
                    }).catch(() => {
                        this.loading = false
                    })
                }
            })
        },
        closeForm() {
            this.$emit('update:show', false)
            this.loading = false
            if (this.$refs['form'] !== undefined) {
                this.$refs['form'].resetFields()
            }
        }
    }
}
</script>
<style lang="less" rel="stylesheet/scss">
  .uploadBtn {
    position: absolute;
    left: 726px;
    /*left: 620px;*/
    top: 5px;
    font-size: 20px;
    cursor: pointer;
    color: #999;
  }

  .uploadBtn:hover {
    color: #000;
  }

  .w-e-text-container {
    height: 500px !important;
  }

  .w-e-toolbar {
    display: flex;
    flex-direction: row;
    justify-content: flex-start;
    flex-wrap: wrap;
  }
</style>
